package com.example.demo;

import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.client.RestTemplate;

import com.youxin.busfoundation.config.RedisClusterConfig;
import com.youxin.busfoundation.config.RedisSentinelConfig;
import com.youxin.foundation.db.redis.RedisClusterCommand;
import com.youxin.foundation.utils.ConfigurationUtils;
import com.youxin.kafka.KafkaConfig;
import com.youxin.kafka.KafkaConsumerTask;


@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
//@EnableConfigurationProperties({RedisClusterConfig.class,RedisSentinelConfig.class}) 

/**
 * 
 * @author yingde.cao
 *
 */
//@EnableAutoConfiguration
//@EnableJpaRepositories
//@ComponentScan(basePackages ={"com.youxin.es.service"})
//@EntityScan(basePackages ={"com.youxin.es.service"})
//@EnableJpaRepositories(basePackages = "com.youxin.es.service", considerNestedRepositories = true)
//@EnableElasticsearchRepositories(basePackages = {"com.youxin.es.service"})
public class SericeFeignApplication {
	
    private static KafkaConsumerTask kafkaConsumerTask;
    
//    @Autowired
//    private RedisClusterConfig redisClusterConfig;
    
	public static void main(String[] args) {	
		
    	Properties properties=ConfigurationUtils.getProperties("kafka.properties");
    	properties.put("group.id", "test");
    	properties.put("enable.auto.commit", "true");
    	properties.put("auto.commit.interval.ms", "1000");
    	properties.put("session.timeout.ms", "30000");
    	properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    	properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

    	ConfigurableApplicationContext context=SpringApplication.run(SericeFeignApplication.class, args);
  //  	RedisClusterConfig clusterConfig=context.getBean(RedisClusterConfig.class);
//    	kafkaConsumerTask=new KafkaConsumerTask(properties, "test");
//    	kafkaConsumerTask.start();
	}
	
	@PostConstruct
    private void init(){
		try {
			//RedisClusterCommand.initConfig(redisClusterConfig);
		} catch (Exception e) {
			// TODO: handle exception
		}
//		Properties properties=ConfigurationUtils.getProperties("kafka.properties");
//    	properties.put("group.id", "test");
      //  logger.info("Spring Boot - @ConfigurationProperties annotation example");
       // logger.info(properties.toString());
    }
	
	@Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
	
	
	@Bean(name = "kafkaConfig")
    @Qualifier("kafkaConfig")
    @Primary
    @ConfigurationProperties(prefix = "kafka")
    public KafkaConfig kafkaConfig() {
        return new KafkaConfig();
    }
	
//	@Bean(name = "redisClusterConfig")
//	//@Configuration
//	//@PropertySource("classpath:redis-cluster.properties")
//	//@ConfigurationProperties(prefix = "redis")
//	public RedisClusterConfig redisClusterConfig() {
//		RedisClusterConfig config=new RedisClusterConfig();
//		return config;
//    }
	
	/**
     * 第一个数据源
     * @return 数据源实例
     */
    @Primary
    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
	@ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 第二个数据源
     * @return 数据源实例
     */
    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 第一个JDBC模板
     * @param dataSource dataSource
     * @return JDBC模板
     */
    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(
            @Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 第二个JDBC模板
     * @param dataSource dataSource
     * @return JDBC模板
     */
    @Bean(name = "secondaryJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(
            @Qualifier("secondaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    
}
